package com.lin.config;

import com.lin.entity.MusicData;
import com.lin.entity.vo.MusicVO;
import com.lin.service.MusicDataService;
import com.lin.service.MusicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Set;
/**
 * 开启定时任务
 * 定时从redis中拉取点赞记录，并更新在mysql中。
 * 配置类  注解的形式  独立的线程
 */
@Configuration  //开启配置类
@EnableScheduling //开启定时器任务
@Slf4j
public class FromRedisToMysqlJob {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MusicDataService musicDataService;

    @Autowired
    private MusicService musicService;
    //乐谱点赞idKey常量
    private static final String PRAISE_THUMBS_KEY = "music.thumbs.id.list.key";
    private static final String PRAISE_COLLECTION_KEY = "music.collection.id.list.key";
    private static final String PRAISE_FORWARD_KEY = "music.forward.id.list.key";


    @Scheduled(cron = "*/10 * * * * *")
    public void savePraiseNumFromRedisToMysql(){

        //step1 查看redis中所有缓存的moodId
        Set<String> musicIds = redisTemplate.opsForSet().members(PRAISE_THUMBS_KEY);
        if (CollectionUtils.isEmpty(musicIds)){
            log.info("redis中并未缓存乐谱点赞有关信息...");
            return;
        }
        //step2 查看redis中，每个moodId对应的UserId
        for (String musicId : musicIds) {

            Set<String> userIds = redisTemplate.opsForSet().members(String.valueOf(musicId));

            if (CollectionUtils.isEmpty(userIds)){
                log.info("redis中并未缓存乐谱id为{}的记录...",musicId);
                continue;
            }
            //step3 将redis中每条userId相关的记录，保存在mysql的关联表中
            for (String userId : userIds) {
                MusicData musicData = new MusicData();
                musicData.setType(0);
                musicData.setMusicId(Integer.valueOf(musicId));
                musicData.setUserId(Integer.valueOf(userId));
                musicDataService.setMusic(musicData);
            }
            log.info("redis向mysql中更新关联信息记录，成功...");
            //step4 更新mysql数据表中的点赞记录
            MusicVO musicVO = musicService.getMusicDetail(Integer.valueOf(musicId));
            musicVO.setThumbs(musicVO.getThumbs() +
                    redisTemplate.opsForSet().size(musicId).intValue());
            musicService.updateMusic(musicVO);
            log.info("redis向mysql中更新点赞记录，成功...");

            //step5 清除redis的moodId数据
            redisTemplate.delete(musicId);
            log.info("成功清除redis中的乐谱id{}记录...",musicId);
        }

        //step6 清楚redis的PRAISE KEY
        redisTemplate.delete(PRAISE_THUMBS_KEY);
        log.info("成功清除redis中的点赞KEY:{}",PRAISE_THUMBS_KEY);
    }
    @Scheduled(cron = "*/10 * * * * *")
    public void saveCollNumFromRedisToMysql(){

        //step1 查看redis中所有缓存的moodId
        Set<String> musicIds = redisTemplate.opsForSet().members(PRAISE_COLLECTION_KEY);
        if (CollectionUtils.isEmpty(musicIds)){
            log.info("redis中并未缓存乐谱收藏有关信息...");
            return;
        }
        //step2 查看redis中，每个moodId对应的UserId
        for (String musicId : musicIds) {

            Set<String> userIds = redisTemplate.opsForSet().members(String.valueOf(musicId));

            if (CollectionUtils.isEmpty(userIds)){
                log.info("redis中并未缓存乐谱id为{}的记录...",musicId);
                continue;
            }
            //step3 将redis中每条userId相关的记录，保存在mysql的关联表中
            for (String userId : userIds) {
                MusicData musicData = new MusicData();
                musicData.setType(1);
                musicData.setMusicId(Integer.valueOf(musicId));
                musicData.setUserId(Integer.valueOf(userId));
                musicDataService.setMusic(musicData);
            }
            log.info("redis向mysql中更新关联信息记录，成功...");
            //step4 更新mysql数据表中的点赞记录
            MusicVO musicVO = musicService.getMusicDetail(Integer.valueOf(musicId));
            musicVO.setCollection(musicVO.getCollection() +
                    redisTemplate.opsForSet().size(musicId).intValue());
            musicService.updateMusic(musicVO);
            log.info("redis向mysql中更新收藏记录，成功...");

            //step5 清除redis的moodId数据
            redisTemplate.delete(musicId);
            log.info("成功清除redis中的乐谱id{}记录...",musicId);
        }

        //step6 清楚redis的PRAISE KEY
        redisTemplate.delete(PRAISE_COLLECTION_KEY);
        log.info("成功清除redis中的收藏KEY:{}",PRAISE_COLLECTION_KEY);
    }
    @Scheduled(cron = "*/10 * * * * *")
    public void saveForwardNumFromRedisToMysql(){

        //step1 查看redis中所有缓存的moodId
        Set<String> musicIds = redisTemplate.opsForSet().members(PRAISE_FORWARD_KEY);
        if (CollectionUtils.isEmpty(musicIds)){
            log.info("redis中并未缓存乐谱转发有关信息...");
            return;
        }
        //step2 查看redis中，每个moodId对应的UserId
        for (String musicId : musicIds) {

            Set<String> userIds = redisTemplate.opsForSet().members(String.valueOf(musicId));

            if (CollectionUtils.isEmpty(userIds)){
                log.info("redis中并未缓存乐谱id为{}的记录...",musicId);
                continue;
            }
            //step3 将redis中每条userId相关的记录，保存在mysql的关联表中
            for (String userId : userIds) {
                MusicData musicData = new MusicData();
                musicData.setType(2);
                musicData.setMusicId(Integer.valueOf(musicId));
                musicData.setUserId(Integer.valueOf(userId));
                musicDataService.setMusic(musicData);
            }
            log.info("redis向mysql中更新关联信息记录，成功...");
            //step4 更新mysql数据表中的点赞记录
            MusicVO musicVO = musicService.getMusicDetail(Integer.valueOf(musicId));
            musicVO.setForward(musicVO.getForward() +
                    redisTemplate.opsForSet().size(musicId).intValue());
            musicService.updateMusic(musicVO);
            log.info("redis向mysql中更新转发记录，成功...");

            //step5 清除redis的moodId数据
            redisTemplate.delete(musicId);
            log.info("成功清除redis中的乐谱id{}记录...",musicId);
        }

        //step6 清楚redis的PRAISE KEY
        redisTemplate.delete(PRAISE_FORWARD_KEY);
        log.info("成功清除redis中的转发KEY:{}",PRAISE_FORWARD_KEY);
    }

}
